#include "writeTexture.hpp"
#include "Log.hpp"
using namespace std;
using namespace m3g;

/**
*
*/
vector<unsigned int> writeTexture (M3GWriter* writer, MQDocument mq_doc, const vector<unsigned int>& image_indices)
{
	Log:: print ("Write Texture.\n");

	vector<unsigned int> object_indices;

	int material_count = mq_doc->GetMaterialCount ();

	for (int i = 0; i < material_count; i++) {

		if (image_indices[i] == 0) {
			object_indices.push_back (0);
			continue;
		}

		M3GObject3DStruct      obj;
		M3GTransformableStruct tra;
		M3GTexture2DStruct     tex;
		tex.image_index  = image_indices[i];
        tex.level_filter = M3GTexture2DStruct::FILTER_LINEAR;
        tex.image_filter = M3GTexture2DStruct::FILTER_LINEAR;
        tex.wrapping_s   = M3GTexture2DStruct::WRAP_REPEAT;
        tex.wrapping_t   = M3GTexture2DStruct::WRAP_REPEAT;

		unsigned int object_index;

		writer->startObject        (M3G_TYPE_TEXTURE2D, &object_index);
		writer->writeObject3D      (obj);
		writer->writeTransformable (tra);
		writer->writeTexture2D     (tex);
		writer->endObject          ();

		object_indices.push_back (object_index);
	}

	return object_indices;
}
